<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_yxw/model')->方法名();
*/
namespace jy_yxw;
defined('BY_JYA') or exit('error');
class feelog extends \plugin{
	//$type0infee_log,1tuifee_log,2手动补
	public function upfeelog($feeid,$update='',$type=0){
		if(!$feeid){
			return false;
		}
		if($type<2){
			$feelog=pdo_get('jy_yxw_infee_log',array('id'=>$feeid,'uniacid'=>$_SESSION['uniacid']));
		}
		if($type<=1){
			$isfeelog=pdo_get('jy_yxw_feelog',array('type'=>$type,'infeeid'=>$feeid,'uniacid'=>$_SESSION['uniacid']));
		}elseif($type==3 || $type==4){
			$feelog=pdo_get('jy_yxw_tuifee_log',array('id'=>$feeid,'uniacid'=>$_SESSION['uniacid']));
			if($feelog['infeeid']){
				$infeelog=pdo_get('jy_yxw_infee_log',array('id'=>$feelog['infeeid'],'uniacid'=>$_SESSION['uniacid']));
			}else{
				$infeelog=pdo_get('jy_yxw_member',array('id'=>$feelog['memberid'],'uniacid'=>$_SESSION['uniacid']));
				$infeelog['memberid']=$feelog['memberid'];
				$infeelog['hefee']=0;
				$infeelog['bufee']=0;
				$infeelog['peifee']=0;
			}
			$isfeelog=pdo_get('jy_yxw_feelog',array('type'=>$type,'tuifeeid'=>$feeid,'uniacid'=>$_SESSION['uniacid']));
			$isfeelog2=pdo_get('jy_yxw_feelog',array('type'=>6,'tuifeeid'=>$feeid,'uniacid'=>$_SESSION['uniacid']));
		}elseif($type==2 || $type==7){
			$fenfeelog=pdo_get('jy_yxw_fenfee_log',array('id'=>$feeid,'uniacid'=>$_SESSION['uniacid']));
			$infeelog=pdo_get('jy_yxw_infee_log',array('id'=>$fenfeelog['infeeid'],'uniacid'=>$_SESSION['uniacid']));
			$isfeelog=pdo_get('jy_yxw_feelog',array('type'=>$type,'fenfeeid'=>$feeid,'uniacid'=>$_SESSION['uniacid']));
		}
		if($isfeelog && $update){
			$update['up_time']=time();
			if($isfeelog['workerheid']){
				$update['hestoreid']=pdo_getcolumn('jy_yxw_worker',array('id'=>$isfeelog['workerheid'],'uniacid'=>$_SESSION['uniacid']),'storeid');
			}
			pdo_update('jy_yxw_feelog',$update,array('id'=>$isfeelog['id'],'uniacid'=>$_SESSION['uniacid']));
			if($isfeelog2['id'] && !$update['hefee']){
				//pdo_update('jy_yxw_feelog',$update,array('id'=>$isfeelog2['id'],'uniacid'=>$_SESSION['uniacid']));
			}
			return true;
		}
		if($type<=1){
			//fee1,总部退费分摊,bufee补贴费费用/补贴费用
			$data=array(
				'memberid'=>$feelog['memberid'],
				'storeid'=>$feelog['storeid'],
				'shoustoreid'=>$feelog['shoustoreid'],
				'koubei_storeid'=>$feelog['koubei_storeid'],
				'workerid'=>$feelog['workerid'],
				'worker5id'=>$feelog['worker5id'],
				'worker6id'=>$feelog['worker6id'],
				'workerheid'=>$feelog['workerheid'],
				'up_workerid'=>$feelog['up_workerid'],
				'fee'=>$feelog['fee'],
				'hefee'=>$feelog['hefee'],
				'zhaofee'=>$feelog['zhaofee'],
				'shouhoufee'=>$feelog['shouhoufee'],
				'bufee'=>$feelog['bufee'],
				'zufee'=>$feelog['zufee'],
				'peifee'=>$feelog['peifee'],
				'jiao_time'=>$feelog['jiao_time'],
				'from1_type'=>$feelog['from1_type'],
				'from2_type'=>$feelog['from2_type'],
				'zhanghu'=>$feelog['zhanghu'],
				'infeeid'=>$feelog['id'],
				'up_time'=>time(),
			);
			$data['status']=$feelog['status']==1?1:0;
		}elseif($type==3 || $type==4){
			//fee1,总部退费分摊
			$data=array(
				'memberid'=>$feelog['memberid'],
				'storeid'=>$infeelog['storeid'],
				'shoustoreid'=>$infeelog['shoustoreid'],
				'koubei_storeid'=>$infeelog['koubei_storeid'],
				'workerid'=>$infeelog['workerid'],
				'worker5id'=>$infeelog['worker5id'],
				'worker6id'=>$infeelog['worker6id'],
				'workerheid'=>$infeelog['workerheid'],
				'up_workerid'=>$infeelog['up_workerid'],
				'fee'=>$feelog['tuifee']>0?0-$feelog['tuifee']:$feelog['tuifee'],
				'hefee'=>0-$infeelog['hefee'],
				'zhaofee'=>$feelog['fee3'],
				'shouhoufee'=>$feelog['fee2'],
				'bufee'=>0-$infeelog['bufee'],
				'zufee'=>$feelog['fee1'],
				'peifee'=>0-$infeelog['peifee'],
				'jiao_time'=>$feelog['tuidaytime'],
				'from1_type'=>$infeelog['from1_type'],
				'from2_type'=>$infeelog['from2_type'],
				'zhanghu'=>$feelog['zhanghu'],
				'tuifeeid'=>$feelog['id'],
				'up_time'=>time(),
			);
			$data['status']=$feelog['status']==4?1:0;
			if($feelog['isfee']>0 || $isfeelog2['id']){
				$data2=array(
					'memberid'=>$feelog['memberid'],
					'storeid'=>$infeelog['storeid'],
					'shoustoreid'=>$infeelog['shoustoreid'],
					'koubei_storeid'=>$infeelog['koubei_storeid'],
					'workerid'=>$infeelog['workerid'],
					'worker5id'=>$infeelog['worker5id'],
					'worker6id'=>$infeelog['worker6id'],
					'workerheid'=>$infeelog['workerheid'],
					'up_workerid'=>$infeelog['up_workerid'],
					'fee'=>$feelog['isfee'],
					'zhaofee'=>$feelog['infee3'],
					'shouhoufee'=>$feelog['infee2'],
					'zufee'=>$feelog['infee1'],
					'jiao_time'=>$feelog['indaytime'],
					'from1_type'=>$infeelog['from1_type'],
					'from2_type'=>$infeelog['from2_type'],
					'zhanghu'=>$feelog['inzhanghu'],
					'tuifeeid'=>$feelog['id'],
					'up_time'=>time(),
				);
				$data2['status']=$feelog['status']==4?1:0;
			}
		}else{
			$data=array(
				'memberid'=>$infeelog['memberid'],
				'storeid'=>$infeelog['storeid'],
				'shoustoreid'=>$infeelog['shoustoreid'],
				'koubei_storeid'=>$infeelog['koubei_storeid'],
				'workerid'=>$infeelog['workerid'],
				'worker5id'=>$infeelog['worker5id'],
				'worker6id'=>$infeelog['worker6id'],
				'workerheid'=>$infeelog['workerheid'],
				'up_workerid'=>$fenfeelog['up_workerid'],
				'fee'=>$fenfeelog['isfee'],
				'hefee'=>$infeelog['workerheid']?$fenfeelog['isfee']/2:0,
				'jiao_time'=>$fenfeelog['paydaytime'],
				'from1_type'=>$infeelog['from1_type'],
				'from2_type'=>$infeelog['from2_type'],
				'zhanghu'=>$infeelog['zhanghu'],
				'fenfeeid'=>$fenfeelog['id'],
				'up_time'=>time(),
			);
			$data['status']=$fenfeelog['status']==1?1:0;
		}
		if($isfeelog['id']){
			if($isfeelog['workerheid']){
				$data['hestoreid']=pdo_getcolumn('jy_yxw_worker',array('id'=>$isfeelog['workerheid'],'uniacid'=>$_SESSION['uniacid']),'storeid');
			}
			pdo_update('jy_yxw_feelog',$data,array('id'=>$isfeelog['id'],'uniacid'=>$_SESSION['uniacid']));
			if($isfeelog2['id']){
				pdo_update('jy_yxw_feelog',$data2,array('id'=>$isfeelog2['id'],'uniacid'=>$_SESSION['uniacid']));
			}elseif($data2){
				$data2['uniacid']=$_SESSION['uniacid'];
				$data2['type']=6;
				$data2['remark']='';
				$data2['createtime']=time();
				if($data2['workerheid']){
					$data2['hestoreid']=pdo_getcolumn('jy_yxw_worker',array('id'=>$data2['workerheid'],'uniacid'=>$_SESSION['uniacid']),'storeid');
				}
				pdo_insert('jy_yxw_feelog',$data2);
			}
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['type']=$type;
			$data['remark']='';
			$data['createtime']=time();
			if($data['workerheid']){
				$data['hestoreid']=pdo_getcolumn('jy_yxw_worker',array('id'=>$data['workerheid'],'uniacid'=>$_SESSION['uniacid']),'storeid');
			}
			pdo_insert('jy_yxw_feelog',$data);
			if($data2){
				$data2['uniacid']=$_SESSION['uniacid'];
				$data2['type']=6;
				$data2['remark']='';
				$data2['createtime']=time();
				if($data2['workerheid']){
					$data2['hestoreid']=pdo_getcolumn('jy_yxw_worker',array('id'=>$data2['workerheid'],'uniacid'=>$_SESSION['uniacid']),'storeid');
				}
				pdo_insert('jy_yxw_feelog',$data2);
			}
		}
		return true;
	}
	public function delfeelog($infee_log=array()){
		pdo_delete('jy_yxw_feelog',array('uniacid'=>$_SESSION['uniacid'],'infeeid'=>$infee_log['id']));
		return true;
	}
}
?>